package com.kika.obwiki.rocketmq;

import com.kika.obwiki.websocket.WebSocketServer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 点赞消息队列监听器
 * 监听VOTE_TOPIC，收到消息后通过WebSocket推送
 */
@Service
@RocketMQMessageListener(consumerGroup = "default", topic = "VOTE_TOPIC")
public class VoteTopicConsumer implements RocketMQListener<MessageExt> {

  private static final Logger LOG = LoggerFactory.getLogger(VoteTopicConsumer.class);

  @Resource
  public WebSocketServer webSocketServer;

  /**
   * 消息处理方法
   * 
   * @param messageExt 消息体
   */
  @Override
  public void onMessage(MessageExt messageExt) {
    byte[] body = messageExt.getBody();
    String msg = new String(body);
    LOG.info("ROCKETMQ收到消息：{}", msg);
    webSocketServer.sendInfo(msg);
  }
}